home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 178_01 / tvx_2.c < prev    next >
C/C++ Source or Header  |  1986-01-16  |  35KB  |  1,437 lines

  1. /* -------------------------------- tvx_2.c ------------------------------- */
  2. /* ========================================================================
  3.  
  4.     tvx_2.c - Part 2 of main TVX code 
  5.  
  6. ============================================================================ */
  7.  
  8. #include "tvx_defs.ic"        /* note tv_defs will #include stdio.h */
  9. #include "tvx_glbl.ic"
  10.  
  11. /* =============================>>> KILLIN <<<============================= */
  12.   killin(cnt)
  13.   int cnt;
  14.   { /* ##  killin - kill cnt lines */
  15.  
  16.     SLOW int i,lim;
  17.     SLOW int from,to,ityp,istrt;
  18.  
  19.     if (cnt+curlin >= nxtlin || (curlin == nxtlin-1 && cnt >= 0))
  20.       {             /* special case: deleting rest of buffer */
  21.     svklin(nxtlin-1);
  22.     for (i = curlin ; i <= nxtlin-1 ; )
  23.         kline(*(lines+i++));
  24.     nxtlin = curlin--;
  25.     if (curlin > 0)
  26.       {
  27.         curchr = *(lines+curlin)+1;
  28.         newscr();
  29.       }
  30.     else
  31.       {
  32.         curchr=0;
  33.         tvclr();
  34.       }
  35.     return;
  36.       }
  37.  
  38.     if (cnt < 0)        /* negative kill */
  39.       {
  40.     cnt = min(-cnt,curlin-1);    /* all upwards? */
  41.     dwnlin(-cnt);        /* go up that far */
  42.       }
  43.  
  44.     if (cnt != 0)
  45.       {
  46.     range(cnt,&to,&from);    /* calculate the line numbers to kill */
  47.  
  48.     curlin=to;        /* remember new current line */
  49.  
  50.     svklin(from);    /* save one line */
  51.     for (i = to ; i <= from ; )        /* mark lines deleted */
  52.         kline(*(lines+i++));
  53.  
  54.     lim = min(nxtlin-1,mxline);
  55.     for (++from ; from <= lim ; )
  56.       {
  57.         *(lines+to++) = *(lines+from++);    /* copy next line number */
  58.       }
  59.  
  60.     nxtlin=to;
  61.     if (nxtlin == curlin)
  62.         --curlin;        /* don't go past end */
  63.     curchr = *(lines+curlin)+1;    /* remember new current character */
  64.  
  65.     if (cnt >= 0 && curlin+(tvlins-tvdlin) < nxtlin &&
  66.       tvdlin < tvlins)    /* killing down */
  67.       {
  68.         tvxy(1,tvy);    /* get to start of line */
  69.         ityp=min(tvlins-tvdlin+1,nxtlin-curlin);
  70.         if (cnt!=1 || !ckline[0])
  71.           {
  72.         tvescr();    /* erase the screen */
  73.         istrt=curlin;
  74.           }
  75.         else
  76.           {
  77.         sendcs(ckline);
  78.         istrt=curlin+ityp-1;
  79.         tvxy(1,tvlins);
  80.         ityp=1;
  81.           }
  82.         tvtype(istrt,ityp);
  83.         tvhdln();    /* home to display line */
  84.       }
  85.     else if ( cnt != 1)    /* neg and > 1 too complicated */
  86.         newscr();            /* kill up, just retype whole screen */
  87.     else if (nxtlin < tvlins)    /* top part of screen */
  88.       {
  89.         if (*ckline)        /* kill line defined */
  90.           {
  91.         tvxy(1,tvy);        /* get to start of line */
  92.         sendcs(ckline);        /* just need to kill the line */
  93.         tvhdln();
  94.           }
  95.         else
  96.         newscr();        /* rewrite it all */
  97.       }
  98.     else if (tvdlin < tvlins)    /* must be in last part of buffer */
  99.       {
  100.         if (*ckline && *ctopb)    /* kill line & topb defined */
  101.           {
  102.         tvxy(1,tvy);        /* get to start of line */
  103.         sendcs(ckline);        /* kill the line */
  104.         if (curlin-tvdlin > 0)    /* something to scroll */
  105.           {
  106.             tvtopb(1);        /* scroll down one line */
  107.             tvtype(curlin-tvdlin,1);    /* type the offscreen line */
  108.             tvdlin++;        /* will start display on next line */
  109.           }
  110.         tvhdln();
  111.           }
  112.         else
  113.         newscr();        /* rewrite it all */
  114.       }
  115.     else        /* if all else fails */
  116.         newscr();
  117.       }
  118.   }
  119.  
  120. /* =============================>>> KLINE  <<<============================= */
  121.   kline(ptr)
  122.   BUFFINDEX ptr;
  123.   {  /* kline - kill off the line beginning at buff position ptr */
  124.  
  125.     SLOW BUFFINDEX i;
  126.  
  127.     for (i=ptr; *(buff+i) != ENDLINE ; )    /* insert GARBAGE to kill */
  128.     *(buff+i++)=GARBAGE;
  129.  
  130.     *(buff+i)=GARBAGE;        /* kill the endline */
  131.   }
  132.  
  133. /* =============================>>> KPREV  <<<============================= */
  134.   kprev()
  135.   { /* kprev - kill from cursor to beginning of line */
  136.  
  137.     FAST int chrs;
  138.  
  139.     svklin(curlin);                /* save one line */
  140.     chrs = curchr - *(lines+curlin) - 1;    /* how much to delete */
  141.     if (chrs > 0)
  142.     delnxt(-chrs);    /* won't cause a combine, so don't worry */
  143.   }
  144.  
  145. /* =============================>>> KREST  <<<============================= */
  146.   krest()
  147.   { /* krest - kill the rest of the line, not including cursor and ENDLINE */
  148.  
  149.     SLOW int chrs;
  150.     SLOW BUFFINDEX i;
  151.  
  152.     svklin(curlin);    /* save one line */
  153.     chrs=0;
  154.     for (i=curchr; *(buff+i)!=ENDLINE; ++i)
  155.     ++chrs;     /* count how much to delete */
  156.     if (chrs > 0)
  157.     delnxt(chrs);    /* won't cause combine, so don't worry */
  158.   }
  159.  
  160. /* =============================>>> NEATEN <<<============================= */
  161.   int neaten(count)
  162.   int count;
  163.   {  /* neaten - fill lines to current margin */
  164.  
  165.     SLOW int oldef, i;
  166.     SLOW BUFFINDEX linbeg;
  167.     SLOW int retval;
  168.  
  169.     retval = TRUE;
  170.     oldef = echof;
  171.     if (count > 1)
  172.     echof = FALSE;
  173.     if (wraplm <= 1 || curlin >= nxtlin-1)
  174.     goto l900;        /* work only if wrap limit turned on */
  175.  
  176.     for (i=1 ; i<=count ; ++i)
  177.       {
  178.     beglin();        /* start at beginning of line */
  179.     if (curlin >= nxtlin-1)
  180.         goto l900;
  181.  
  182.     /* don't neaten leading space, cr, period or tab */
  183.  
  184.     if (*(buff+curchr) == '.')
  185.       {
  186.         dwnlin(1);
  187.         continue;        /* skip dot commands */
  188.       }
  189.  
  190.     while (*(buff+curchr)== ' ' || *(buff+curchr)==ENDLINE
  191.       || *(buff+curchr) == 9)
  192.       {
  193.         right(1);    /* skip this line */
  194.       }
  195.  
  196.     do
  197.       {
  198.         if (*(buff+curchr) == ENDLINE)
  199.           {
  200.         if (tvx+leftmg < wraplm)    /* combine lines! */
  201.           {
  202.             linbeg = *(lines+curlin+1)+1;
  203.             /* pt to first char of next line */
  204.             if (*(buff+linbeg) == ' ' || *(buff+linbeg) == ENDLINE
  205.               || *(buff+linbeg) == 9 || *(buff+linbeg) == '.')
  206.               {
  207.             dwnlin(1);
  208.             break;    /* no more combining */
  209.               }
  210.             if (! neat1(1,32))
  211.             goto l990;
  212.             goto NEATNEXT;    /* tab over another word */
  213.           }
  214.         else
  215.           {
  216.             dwnlin(1);    /* no more combining on line */
  217.             break;
  218.           }
  219.           }
  220.  
  221. NEATNEXT:
  222.         if (*(buff+curchr-1)==' ' && tvx+leftmg >= wraplm)    /* change to cr */
  223.           {
  224.         if (! neat1(-1,CR))    /* delete the blank */
  225.             goto l990;
  226.         break;
  227.           }
  228.         wordr(1);
  229.       } /*# end of the repeat */
  230.     while (1);
  231.       } /*# end of the for     */
  232. l900:
  233.     echof = oldef;
  234.     if (oldef && count > 1)
  235.     newscr();
  236.     return (retval);
  237.  
  238. l990:                /* failure return */
  239.     retval = FALSE;
  240.     goto l900;
  241.   }
  242.  
  243. /* =============================>>> NEAT1  <<<============================= */
  244.   neat1(dir, val)
  245.   int dir, val;
  246.   {  /* change character dir to val */
  247.  
  248.     SLOW int oldwrp;
  249.  
  250.     oldwrp = wraplm;
  251.     wraplm = 0;
  252.     if (! delnxt(dir))
  253.     goto l900;
  254.     if (! ins_chr(val))
  255.     goto l900;
  256.     wraplm = oldwrp;
  257.     return (TRUE);
  258. l900:
  259.     wraplm = oldwrp;
  260.     return (FALSE);
  261.   }
  262.  
  263. /* =============================>>> NEWSCR <<<============================= */
  264.   newscr()
  265.   { /* newscr - retype entire screen, updating cursor position if necessary */
  266.  
  267.    SLOW int ibeg,cnt;
  268.  
  269.     if (tvlins != tvhardlines || nxtlin-1 <= tvlins)
  270.     /* two kinds of screen rewrite */
  271.     tvclr();            /* clear the screen and home */
  272.     else
  273.     tvxy(1,1);
  274.  
  275.     finddl(&ibeg,&cnt);     /* calculate where it will go */
  276.     tvtype(ibeg,cnt);        /* type it out */
  277.     tvhdln();            /* home to display line */
  278.   }
  279.  
  280. /* =============================>>> OPENLN <<<============================= */
  281.   openln(cnt)
  282.   int cnt;
  283.   {  /* openln - open a new line */
  284.  
  285.     FAST int i;
  286.     SLOW int pcnt, oldauto;
  287.  
  288.     oldauto = autoin; autoin = FALSE;    /* don't allow autoindent */
  289.     pcnt = cnt >= 0 ? cnt : (-cnt);    /* only allow positive opens */
  290.     for (i=1; i<=pcnt; ++i)
  291.     ins_chr(CR);    /* insert right number of newlines */
  292.     dwnlin(-pcnt);    /* and goto beginning of the opened line */
  293.     endlin();
  294.     autoin = oldauto;
  295.   }
  296.  
  297. /* =============================>>> RANGE  <<<============================= */
  298.   range(cnt,lbeg,lend)
  299.   int cnt,*lbeg,*lend;
  300.   { /* determine a legal line number range given cnt */
  301.  
  302.     if (cnt <= 0)
  303.       {
  304.     *lbeg=max(curlin+cnt,1);
  305.     *lend=curlin;
  306.     if (cnt < 0)
  307.        *lend = (*lend)-1;
  308.       }
  309.     else
  310.       {
  311.     *lbeg=curlin;
  312.     *lend=min(nxtlin-1,curlin+cnt-1);
  313.       }
  314.  }
  315.  
  316. /* =============================>>> RIGHT  <<<============================= */
  317.   right(cnt)
  318.   int cnt;
  319.   {  /* move cursor right cnt characters
  320.     newlines count as one character */
  321.  
  322.     FAST int change,i;
  323.